$(function() {
    //主区域
    var mainCtrl = avalon.define({
        $id: "mainController",
        tagList: [],
        adding: false,
        //显示删除弹框
        deltagpop: function(tag) {
            popCtrl.deltag = tag;
            popCtrl.confirmType = "del";
            $("#popup_confirm").show();
        },
        //新增标签
        addTag: function() {
            if (mainCtrl.adding || mainCtrl.tagList.length == 20) {
                return;
            }
            mainCtrl.adding = true;
            mainCtrl.tagList.unshift({
                id: "",
                name: " "
            });
            setTimeout(function() {
                $("td .u-ipt").focus();
            }, 0);
        },
        //保存新增的标签
        saveTag: function() {
            $("td .u-ipt").css("border-color", "");
            $(".tag-edit .err-box").remove();
            var iptVal = $("td .u-ipt").val();
            if (iptVal == "") {
                $("td .u-ipt").css("border-color", "#ff0000").focus().after('<div class="err-box"><p class="u-error">请输入标签名称</p></div>');
            } else {
                var dataJson = JSON.stringify({"name": iptVal,"type": "1"});
                $.ajax({
                    type: "post",
                    dataType: "json",
                    data: {
                        "json": dataJson
                    },
                    url: DI.tagGradeupsert,
                    success: function(data) {
                        if (data.retFlag == 1) {
                            mainCtrl.adding = false;
                            searchTag();
                            PLATFORM.showTips("success", "新增成功");
                        } else {
                            $("td .u-ipt").css("border-color", "#ff0000").after('<div class="err-box"><p class="u-error">' + data.errorMessage + '</p></div>');
                        }
                    },
                    error: function(err) {
                        console.log(err);
                    }
                });
            }
        },
        noSave: function() {
            mainCtrl.adding = false;
            mainCtrl.tagList.removeAt(0);
        }

    });
    //弹框区域
    var popCtrl = avalon.define({
        $id: "popController",
        deltag: "",
        confirmType: "del",
        delTag: function() {
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "id": popCtrl.deltag.id
                },
                url: DI.tagGradedelete,
                success: function(data) {
                    $("#popup_confirm").hide();
                    if (data.retFlag == 1) {
                        searchTag();
                        PLATFORM.showTips("success", "删除成功");
                       // mainCtrl.tagList.remove(popCtrl.deltag);
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        }
    });

    //初始化数据
    searchTag();

    //查询标签数据
    function searchTag() {
        $.ajax({
            type: "post",
            dataType: "json",
            data: {
                "type": 1
            },
            url: DI.tagGradequery,
            success: function(data) {
                mainCtrl.tagList = data.data;
            },
            error: function(err) {
                console.log(err);
            }
        });
    }

    avalon.scan();
});